package com.fofia.iotweb.web.controllers.api;


import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.fofia.iotweb.Contants.SolonBootConfigDto;
import com.fofia.iotweb.cache.GatewayCacheHelper;
import com.fofia.iotweb.web.domain.service.GatewayService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import org.noear.solon.annotation.*;

@Controller
@Mapping("GatewayApi")
public class GatewayApiController {


    @Inject
    private GatewayCacheHelper gateCacheHelper;
    @Inject
    private GatewayService gatewayService;
    @Inject
    private RequestHelper requestHelper;

    @Inject
    private SolonBootConfigDto solonBootConfigDto;
    /**
     * 分页数据，时间降序
     * @return
     */
    @Post
    @Mapping("getPageList")
    public DataTableDto getPageList() {

        String gatewayId=requestHelper.getString("gatewayId","");
        int page=requestHelper.getInt("page",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        if(solonBootConfigDto.isApilog()){
            StaticLog.info("来自Ip:{},getPageList:{} {} {} {},{}",requestHelper.getIp(),gatewayId,page,pageSize,stime,etime);
        }

        if(StrUtil.isEmpty(gatewayId)){
            return JsonResult.table("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.table("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.table("请输入结束时间");
        }

        try{
            var list=gatewayService.getPageList(gatewayId,page,pageSize,stime,etime);
            return list;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.table();
    }


    @Get
    @Mapping("test")
    public ResultAdaptDto test(){
        return JsonResult.success("未授权访问通过");
    }
}
